// cQPSettingsPage.cpp : implementation file
//

#include "stdafx.h"
#include "QPMig.h"
#include "cNotesSettingsPage.h"
#include "helper.h"
#include "cQuickPlaceDB.h"
extern CQPMigApp theApp;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// cQPSettingsPage property page

IMPLEMENT_DYNCREATE(cQPSettingsPage, CPropertyPageEx)

cQPSettingsPage::cQPSettingsPage() : CPropertyPageEx(cQPSettingsPage::IDD,0,IDS_NOTESSETTINGSHEADERTITLE,IDS_NOTESSETTINGSHEADERSUBTITLE)
{
	//{{AFX_DATA_INIT(cQPSettingsPage)
	m_strNotesIni = theApp.stControl.m_strNotesIni;//_T("c:\\Lotus\\Domino\\kimmo.ini");
	m_strNotesUserName = _T("");
	m_strNotesPassword = _T("");
	#ifdef DEBUG
	m_strNotesPassword = _T("QPPassword");
	#endif
	m_strDataPath = theApp.stControl.m_strDirectoryPath;
	m_strQuickPlacePath = theApp.stControl.m_strQuickPlacePath;
	m_bDirectorySecurity = theApp.stControl.m_bDirectorySecurity;
	m_chkAdvanced = FALSE;
	m_bMergeFolders = theApp.stControl.m_bMergeFolders;
	m_strDocumentLibraryName = _T("");
	m_strSiteTemplate = _T("");
	m_bSubRoomListing = theApp.stControl.m_bSubRoomListing;
	m_bUseDescriptionForBody = theApp.stControl.m_bUseDescriptionForBody;
	m_bUseFolderForMoreThanTwoAttachments = theApp.stControl.m_bUseFolderForMoreThanTwoAttachments;
	m_bUseFoldersForAllItems=theApp.stControl.m_bUseFoldersForAllItems;
	m_strCustomProperties = theApp.stControl.m_strCustomProperties;
	//}}AFX_DATA_INIT
}

cQPSettingsPage::~cQPSettingsPage()
{
}

void cQPSettingsPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPageEx::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(cQPSettingsPage)
	DDX_Text(pDX, IDC_EDIT1, m_strNotesIni);
	DDX_Text(pDX, IDC_EDIT2, m_strNotesUserName);
	DDX_Text(pDX, IDC_EDIT3, m_strNotesPassword);
	DDX_Text(pDX, IDC_EDIT6, m_strDataPath);
	DDX_Text(pDX, IDC_EDIT9, m_strQuickPlacePath);
	DDX_Check(pDX, IDC_CHECK1, m_bDirectorySecurity);
	DDX_Check(pDX, IDC_CHKADVANCED, m_chkAdvanced);
	DDX_Check(pDX, IDC_CHKMERGEFOLDERS, m_bMergeFolders);
	DDX_Text(pDX, IDC_EDIT4, m_strDocumentLibraryName);
	DDX_Text(pDX, IDC_EDIT8, m_strSiteTemplate);
	DDX_Check(pDX, IDC_CHECK3, m_bSubRoomListing);
	DDX_Check(pDX, IDC_CHECK5, m_bUseDescriptionForBody);
	DDX_Check(pDX, IDC_CHECK4, m_bUseFolderForMoreThanTwoAttachments);
	DDX_Check(pDX, IDC_CHECK8, m_bUseFoldersForAllItems);
	DDX_Text(pDX, IDC_EDIT10, m_strCustomProperties);
	DDX_Control(pDX, IDC_LOCALECHOOSER, m_cbSupportedLocales);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(cQPSettingsPage, CPropertyPageEx)
	//{{AFX_MSG_MAP(cQPSettingsPage)
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDC_CHKMERGEFOLDERS, OnChkmergefolders)
	ON_BN_CLICKED(IDC_CHKADVANCED, OnChkadvanced)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// cQPSettingsPage message handlers

void cQPSettingsPage::OnBrowse() 
	{
	// BLOCK - inserted by CFileDialog macro
		CFileDialog dlg(TRUE, "", "",  OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,  "Notes Ini files (*.ini)|*.ini|All Files (*.*)|*.*||", this);
 
		dlg.m_ofn.lpstrTitle = "Open";

		if (dlg.DoModal() == IDOK)
			{
				m_strNotesIni=dlg.GetPathName();
		}
		UpdateData(FALSE);

	
}

LRESULT cQPSettingsPage::OnWizardNext() 
{
	theApp.stControl.pDebugLog->LogOneLine("Entering cQPSettingsPage::OnWizardNext()");
	theApp.stControl.pDebugLog->Flush();
	UpdateData();
	theApp.stControl.m_bAdvanced=m_chkAdvanced;
	theApp.stControl.m_strDirectoryPath=m_strDataPath;
	theApp.stControl.m_strQuickPlacePath=m_strQuickPlacePath;
	theApp.stControl.m_bMergeFolders=m_bMergeFolders;
	theApp.stControl.m_strDocumentLibraryName=m_strDocumentLibraryName;
	theApp.stControl.bstrSiteTemplate=m_strSiteTemplate.AllocSysString();
	theApp.stControl.m_bSubRoomListing=m_bSubRoomListing;
	theApp.stControl.m_bUseDescriptionForBody=m_bUseDescriptionForBody;
	theApp.stControl.m_bUseFolderForMoreThanTwoAttachments=m_bUseFolderForMoreThanTwoAttachments;
	theApp.stControl.m_bUseFoldersForAllItems=m_bUseFoldersForAllItems;
	
	theApp.stControl.m_strCustomProperties = m_strCustomProperties ;
	theApp.stControl.m_nLocale=m_cbSupportedLocales.GetItemData(m_cbSupportedLocales.GetCurSel());

	if(m_strCustomProperties.GetLength()>0)
		{
			CFile theFile;
			TRY
				{
					if(theFile.Open(m_strCustomProperties, CFile::modeRead))
						{
							CArchive archive(&theFile, CArchive::load);


							theApp.stControl.m_objArrayCustomFields.Serialize(archive);
							archive.Flush();
							archive.Close();
							theFile.Close();
						}
					else
						{
							CString cInfo;
							cInfo.Format("Could not open the file %s",m_strCustomProperties);
							theApp.stControl.pLog->LogOneLine(cInfo);
							
							
						}

				}
			CATCH( CFileException, e )
				{
					if( e->m_cause == CFileException::fileNotFound )
						MessageBox( "ERROR: File not found\n");
					else
						e->ReportError();
				}
			END_CATCH

		}
	if(theApp.stControl.m_strDirectoryPath.GetLength()==0)
		{
			CString cInfo;
			cInfo.Format("You need to specify the local data path" );
			AfxMessageBox(cInfo,MB_OK|MB_ICONERROR);
			return -1;
			
		}
	DWORD dwErrCode=0;
	if(!CreateQPDirectory(CString(theApp.stControl.m_strDirectoryPath),&dwErrCode))
		{
			LPVOID lpMsgBuf;
			FormatMessage( 
					FORMAT_MESSAGE_ALLOCATE_BUFFER | 
					FORMAT_MESSAGE_FROM_SYSTEM | 
					FORMAT_MESSAGE_IGNORE_INSERTS,
					NULL,
					dwErrCode,
					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
					(LPTSTR) &lpMsgBuf,
					0,
					NULL 
			);
			
			CString cInfo;
			cInfo.Format("Could not create the directory %s\r\nThe error message is %s",theApp.stControl.m_strDirectoryPath,lpMsgBuf );
			AfxMessageBox(cInfo,MB_OK|MB_ICONERROR);
			theApp.stControl.pLog->LogOneLine(cInfo);
			// Free the buffer.
			LocalFree(lpMsgBuf);
			return -1;

		}
	if("\\"!=theApp.stControl.m_strDirectoryPath.Right(1))
		theApp.stControl.m_strDirectoryPath+="\\";
	
	theApp.stControl.m_strNotesIni=m_strNotesIni;
	theApp.stControl.m_strNotesPwd=m_strNotesPassword;
	theApp.stControl.m_bDirectorySecurity=m_bDirectorySecurity;
	theApp.stControl.pDebugLog->LogOneLine("Exiting cQPSettingsPage::OnWizardNext()");
	theApp.stControl.pDebugLog->Flush();
	
 	if(InitializeNotes())
		return CPropertyPageEx::OnWizardNext();
	else
		return -1;
}

BOOL cQPSettingsPage::InitializeNotes()
	{
		theApp.stControl.pDebugLog->LogOneLine("Entering cQPSettingsPage::InitializeNotes()");
		theApp.stControl.pDebugLog->Flush();
		int iRet = NO_ERROR;
		BOOL bRet=FALSE;
		CHAR	szBuf[ 16 ];
		DWORD	cbBuf = sizeof(szBuf);
		CString cTempPath;

		//Validate that it is a notes.ini file
		if (GetPrivateProfileSection( "Notes", szBuf, cbBuf, m_strNotesIni) == 0)
		{
			AfxMessageBox("Incorrect Notes.ini specified",MB_OK|MB_ICONERROR);
			return FALSE;
		}
		if(theApp.stControl.fIsNotesInitialized)
			{
				for(int i=0;i<=theApp.stControl.m_objQuickPlaces.GetUpperBound();i++)
					{
						cQuickPlaceDB *pdb=(cQuickPlaceDB *)theApp.stControl.m_objQuickPlaces.GetAt(i);
						delete pdb;
					}
			
				if(theApp.stControl.NABdb.IsOpen())
					theApp.stControl.NABdb.Close();
				
				theApp.stControl.session.Term(TRUE);
			}



		// Copy to a temporary location
		if (theApp.stControl.m_strTempNotesIni.IsEmpty())
			{
				GetTempPath( MAX_PATH - 12, cTempPath.GetBuffer(MAX_PATH-12) );
				cTempPath.ReleaseBuffer();
				GetTempFileName( cTempPath, "INI", 0, theApp.stControl.m_strTempNotesIni.GetBuffer(MAX_PATH));
				TRACE(theApp.stControl.m_strTempNotesIni);
			}

		DeleteFile( theApp.stControl.m_strTempNotesIni);
		if (!CopyFile( m_strNotesIni, theApp.stControl.m_strTempNotesIni, FALSE ))
			{
				AfxMessageBox("Couldn't copy the Notes.ini file",MB_OK|MB_ICONERROR);
				return FALSE;
			}
		SetFileAttributes( theApp.stControl.m_strTempNotesIni, FILE_ATTRIBUTE_NORMAL );

		if (!WritePrivateProfileString( "Notes", "EXTMGR_ADDINS", "NotesPwdDll.dll", theApp.stControl.m_strTempNotesIni ))
			return FALSE;




		theApp.stControl.hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE,    // Current file handle. 
				NULL,                              // Default security. 
				PAGE_READWRITE,                    // Read/write permission. 
				0,                                 // Max. object size. 
				1024,                                 // Size of hFile. 
				"STSNotesPwd");            // Name of mapping object. 

		if (theApp.stControl.hMapFile == NULL) 
			{ 
				AfxMessageBox("Could not create file-mapping object."); 
				return FALSE;
			} 

		theApp.stControl.lpMapAddress = MapViewOfFile(theApp.stControl.hMapFile, // Handle to mapping object. 
				FILE_MAP_ALL_ACCESS,               // Read/write permission 
				0,                                 // Max. object size. 
				0,                                 // Size of hFile. 
				0); 
		// Map entire file. 

		if (theApp.stControl.lpMapAddress == NULL) 
			{ 
				AfxMessageBox("Could not map view of file."); 
				return FALSE;
			} 
		memset(theApp.stControl.lpMapAddress,'\0',1024);
		memcpy(theApp.stControl.lpMapAddress,m_strNotesPassword,m_strNotesPassword.GetLength());

		BOOL bChanged=FALSE;
		if(!theApp.stControl.m_bThrowNotesErrors)
			{
				theApp.stControl.m_bThrowNotesErrors=TRUE;
				LNSetThrowAllErrors( theApp.stControl.m_bThrowNotesErrors);
				bChanged=TRUE;
			}

		try   // Start of try block
			{
			LNString User;
			theApp.stControl.session.Init(theApp.stControl.m_strTempNotesIni);   // Initialize the API
			theApp.stControl.fIsNotesInitialized=TRUE;
			m_strNotesUserName=theApp.stControl.session.GetUserName();
			bRet=TRUE;
			LNDatabaseArray dbserverarray;
			if(LNNOERROR==theApp.stControl.session.GetAddressBooks( &dbserverarray, theApp.stControl.session.GetEnvironmentString("MAILSERVER")))
				{
				theApp.stControl.NABdb=dbserverarray [0];
				}
			
			theApp.stControl.NABdb.Open();
			LNInternational intl=theApp.stControl.session.GetInternational() ;
			
			UpdateData(FALSE);
			}

		catch(LNSTATUS error) // Handle any thrown exceptions 
			{
			char errorBuf[LNERROR_MESSAGE_LENGTH];
			
			LNGetErrorMessage(error, errorBuf);
			AfxMessageBox(errorBuf,MB_OK|MB_ICONERROR);
			theApp.stControl.pDebugLog->LogOneLine(errorBuf);
			}

		if(bChanged)
			{
			theApp.stControl.m_bThrowNotesErrors=FALSE;
			LNSetThrowAllErrors( theApp.stControl.m_bThrowNotesErrors);
			}
		theApp.stControl.pDebugLog->LogOneLine("Exiting cQPSettingsPage::InitializeNotes()");
		theApp.stControl.pDebugLog->Flush();

		return bRet;
		
		
	}

BOOL cQPSettingsPage::OnSetActive() 
{
	UpdateData(FALSE);
	if(m_cbSupportedLocales.GetCount()<1)
	{
		int nID=m_cbSupportedLocales.AddString("Arabic");
		m_cbSupportedLocales.SetItemData(nID,1025);
		nID=m_cbSupportedLocales.AddString("Chinese - Hong Kong SAR");
		m_cbSupportedLocales.SetItemData(nID,3076);
		nID=m_cbSupportedLocales.AddString("Chinese - Simplified");
		m_cbSupportedLocales.SetItemData(nID,2052);
		nID=m_cbSupportedLocales.AddString("Chinese - Traditional");
		m_cbSupportedLocales.SetItemData(nID,1028);
		nID=m_cbSupportedLocales.AddString("Czech");
		m_cbSupportedLocales.SetItemData(nID,1029);
		nID=m_cbSupportedLocales.AddString("Danish");
		m_cbSupportedLocales.SetItemData(nID,1030);
		nID=m_cbSupportedLocales.AddString("Dutch");
		m_cbSupportedLocales.SetItemData(nID,1043);
		
		
		nID=m_cbSupportedLocales.AddString("English");
		int def=nID;
		m_cbSupportedLocales.SetItemData(nID,1033);
		nID=m_cbSupportedLocales.AddString("Finnish");
		m_cbSupportedLocales.SetItemData(nID,1035);
		nID=m_cbSupportedLocales.AddString("French");
		m_cbSupportedLocales.SetItemData(nID,1036);
		nID=m_cbSupportedLocales.AddString("German");
		m_cbSupportedLocales.SetItemData(nID,1031);
		nID=m_cbSupportedLocales.AddString("Greek");
		m_cbSupportedLocales.SetItemData(nID,1032);
		nID=m_cbSupportedLocales.AddString("Hebrew");
		m_cbSupportedLocales.SetItemData(nID,1037);
		nID=m_cbSupportedLocales.AddString("Hungarian");
		m_cbSupportedLocales.SetItemData(nID,1038);
		nID=m_cbSupportedLocales.AddString("Italian");
		m_cbSupportedLocales.SetItemData(nID,1040);
		nID=m_cbSupportedLocales.AddString("Japanese");
		m_cbSupportedLocales.SetItemData(nID,1041);
		nID=m_cbSupportedLocales.AddString("Korean");
		m_cbSupportedLocales.SetItemData(nID,1042);
		nID=m_cbSupportedLocales.AddString("Norwegian");
		m_cbSupportedLocales.SetItemData(nID,1044);
		nID=m_cbSupportedLocales.AddString("Polish");
		m_cbSupportedLocales.SetItemData(nID,1045);
		nID=m_cbSupportedLocales.AddString("Portuguese");
		m_cbSupportedLocales.SetItemData(nID,2070);
		nID=m_cbSupportedLocales.AddString("Portuguese - Brazilian");
		m_cbSupportedLocales.SetItemData(nID,1046);
		nID=m_cbSupportedLocales.AddString("Russian");
		m_cbSupportedLocales.SetItemData(nID,1049);
		nID=m_cbSupportedLocales.AddString("Spanish");
		m_cbSupportedLocales.SetItemData(nID,1034);
		nID=m_cbSupportedLocales.AddString("Swedish");
		m_cbSupportedLocales.SetItemData(nID,1053);
		nID=m_cbSupportedLocales.AddString("Thai");
		m_cbSupportedLocales.SetItemData(nID,1054);
		nID=m_cbSupportedLocales.AddString("Turkish");
		m_cbSupportedLocales.SetItemData(nID,1055);
		m_cbSupportedLocales.SetCurSel(def);
	}
	
	return CPropertyPageEx::OnSetActive();
}


void cQPSettingsPage::OnChkmergefolders() 
{
	UpdateData(TRUE);	

}

void cQPSettingsPage::OnChkadvanced() 
{
	UpdateData(TRUE);	
	if(m_chkAdvanced)
		{
			GetDlgItem(IDC_STATICQPPATH)->ShowWindow(SW_SHOWNOACTIVATE);
			GetDlgItem(IDC_CHKMERGEFOLDERS)->ShowWindow(SW_SHOWNOACTIVATE);
			GetDlgItem(IDC_EDIT9)->ShowWindow(SW_SHOWNOACTIVATE);
			GetDlgItem(IDC_EDIT4)->ShowWindow(SW_SHOWNOACTIVATE);
			GetDlgItem(IDC_LABEL8)->ShowWindow(SW_SHOWNOACTIVATE);
			GetDlgItem(IDC_EDIT8)->ShowWindow(SW_SHOWNOACTIVATE);
			GetDlgItem(IDC_CHECK3)->ShowWindow(SW_SHOWNOACTIVATE);
			GetDlgItem(IDC_CHECK4)->ShowWindow(SW_SHOWNOACTIVATE);
			GetDlgItem(IDC_CHECK5)->ShowWindow(SW_SHOWNOACTIVATE);
			GetDlgItem(IDC_CHECK8)->ShowWindow(SW_SHOWNOACTIVATE);
		}
	else
		{
			GetDlgItem(IDC_STATICQPPATH)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_CHKMERGEFOLDERS)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_EDIT9)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_EDIT4)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_LABEL8)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_EDIT8)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_CHECK3)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_CHECK4)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_CHECK5)->ShowWindow(SW_HIDE);
			GetDlgItem(IDC_CHECK8)->ShowWindow(SW_HIDE);
		}

	
}

//DEL void cQPSettingsPage::OnBrowse3() 
//DEL {
//DEL 		CFileDialog dlg(TRUE, "", "", OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,  "All Files (*.*)|*.*||", this);
//DEL  
//DEL 		dlg.m_ofn.lpstrTitle = "Choose the extract directory";
//DEL 
//DEL 		if (dlg.DoModal() == IDOK)
//DEL 			{
//DEL 				m_strDataPath=dlg.GetFolderPath();
//DEL 			}
//DEL 		UpdateData(FALSE);
//DEL 	
//DEL }
